package yxy.game.pm2.command.chibi;

import com.alibaba.fastjson.JSONArray;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ChibiMessage;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.chibi.ChibiHandle;

import java.util.ArrayList;

/**
 * 火烧赤壁怪物战力和奖励返回
 */
@MessageType(proto = Command.ChibiMonsterPower, classtype = ChibiMessage.ChibiMonsterPowerRequest.class)
public class CMD_ChibiMonsterPower extends Command<ChibiMessage.ChibiMonsterPowerRequest> {

    @Override
    public void execute(MessageSource messageSource, ChibiMessage.ChibiMonsterPowerRequest request) {

        try {
            ChibiMessage.ChibiMonsterPowerResponse.Builder response = ChibiMessage.ChibiMonsterPowerResponse.newBuilder();

            String idList = request.getIdList();

            JSONArray jsonArray = JSONArray.parseArray(idList);

            JSONArray returnArrary = new JSONArray();
            ChibiHandle chibiHandle = new ChibiHandle();
            for (int i = 0; i < jsonArray.size(); i++) {
                long power = 0;
                int id = jsonArray.getIntValue(i);
                ArrayList<FighterModule> fighterModuleList = chibiHandle.getFighterModuleList(id);
                for (FighterModule fighterModule : fighterModuleList) {
                    power += fighterModule.getTotalPower();
                }
                returnArrary.add(power);
            }

            response.setPowerArrary(returnArrary.toString());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void send(MessageSource messageSource, ChibiMessage.ChibiMonsterPowerResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}